package com.inspinia.upms.common.model;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.inspinia.upms.common.model.user.UpmsUserStatus;

import java.io.IOException;


/**
 * 枚举转换类
 * 使用方法：在实体类对象上面添加注解@JsonDeserialize(using = ResourcesTypeJsonDeserializer.class)
 * Created by zhangyuan on 2017/1/18.
 */
public class UpmsUserStatusDeserialize extends JsonDeserializer{

    @Override
    public UpmsUserStatus deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        JsonNode node = jp.getCodec().readTree(jp);
        JsonNode jsonNode = node.get("code");
        if(null != jsonNode){
            int code = jsonNode.intValue();
            return UpmsUserStatus.valueOf(code);
        }else {
            String text = node.asText();
            return UpmsUserStatus.valueOf(Integer.valueOf(text));
        }
    }
}
